package com.cancer.common.util;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public abstract class AbstractTrasport {
	private String url;

	@Autowired
	private RestTemplate restTemplate;

	public String get(String url) {
		ResponseEntity<String> responseEntity = this.restTemplate.getForEntity(url, String.class, new Object[0]);

		return (String) responseEntity.getBody();
	}

	public String get(String url, Map<String, String> params) {
		return (String) this.restTemplate.getForObject(url, String.class, params);
	}

	public Map getWithMapResponse(String url) {
		ResponseEntity<Object> responseEntity = this.restTemplate.getForEntity(url, Object.class, new Object[0]);
		return (Map) responseEntity.getBody();
	}

	public String post(String url) {
		MultiValueMap<?, ?> headers = new LinkedMultiValueMap<Object, Object>();

		ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, headers, String.class,
				new Object[0]);
		return (String) responseEntity.getBody();
	}

	public String post(String url, String params) {
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<String> request = new HttpEntity<String>(params, headers);
		ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, request, String.class,
				new Object[0]);
		return (String) responseEntity.getBody();
	}

	public Map post(String url, Object params) {
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<Object> request = new HttpEntity<Object>(params, headers);
		ResponseEntity<Object> responseEntity = this.restTemplate.postForEntity(url, request, Object.class,
				new Object[0]);
		return (Map) responseEntity.getBody();
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public RestTemplate getRestTemplate() {
		return this.restTemplate;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}
}
